'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 查询开启出阵士兵格式数
 * @author agl
 */

var async = require('async');
var Code = require('../../../../consts/code');
var buildService = require('../../../../services/buildService');
var taskService = require('../../../../services/taskService');
var roleService = require('../../../../services/roleService');
var soldierConf = require('../../../../config/soldierConf');
var buildType = require('../../../../consts/buildType');
var constantUtil = require('../../../../util/constantUtil');
var PlayerManager = require('../../../../libs/playerManager');
var dataApi = require('../../../../libs/dataApi');

module.exports = function (msg, session, next) {
    var role_id = session.uid;

    var battle_soldier_space_count;
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'build', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var build_model = player_manager.build;

                var soldier_college_build = build_model.getBuildByType(buildType.SOLDIERCOLLEGE);
                battle_soldier_space_count = _getSpaceCount(soldier_college_build.getLv());

                cb();
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    battle_soldier_space_count: battle_soldier_space_count
                }
            });
        }
    );
};

var _getSpaceCount = function (soldier_college_lv) {
    var battle_soldier_space_count = constantUtil.value('start_soldier_count');
    dataApi.soldier_count_open.any(function (open_info) {
        if (soldier_college_lv < open_info.building_lvl) {
            return true;
        }
        battle_soldier_space_count = open_info.id;
        return false;
    });

    return battle_soldier_space_count;
};
